// pages/task/task.js
import activityApi from '../../service/activityApi'
import { reLogin } from '../../service/request'
const app = getApp()
let codeConfig = app.globalData.codeConfig

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hadAuthFlag: wx.getStorageInfoSync('hadAuthFlag'),
    mongoliaFlag: false,
    taskType: '1',
    taskTypeCode: {...codeConfig.taskType},
    whetherFocus: false, // 是否关注公众号
    whetherCollect: false, // 是否收藏小程序
    whetherOpen: false, // 是否开通达人店
    picBaseUrl: app.globalData.picBaseUrl,
    capsulePosition: '', // 胶囊键位置
    loadingFlag: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    this.getHadAuth()
    let capsulePosition = wx.getMenuButtonBoundingClientRect()
    this.setData({
      capsulePosition: capsulePosition.right + (capsulePosition.width * 1.5) + ''
    })
  },

  // 获取用户授权信息
  getHadAuth(){
    let that = this
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          that.setData({
            hadAuthFlag: true
          })
          that.getTask()
        } else {
          that.setData({
            hadAuthFlag: false,
            loadingFlag: false
          })
        }
      }
    })
  },

  // 同意授权-并登陆
  bindGetUserInfo (e) {
    let that = this
    if (!!e.detail.userInfo) {
      wx.setStorageSync('hadAuthFlag', true)
      app.globalData.userInfo = e.detail.userInfo
      app.globalData.hadAuthUserInfo = true
      reLogin().then((res) => {
        app.globalData.refreshIndexFlag = true
        this.setData({
          hadAuthFlag: true
        })
        that.getTask()
      })
    } else {
      wx.setStorageSync('hadAuthFlag', false)
      this.setData({
        hadAuthFlag: false
      })
    }
  },
  // 查询任务列表
  getTask() {
    let that = this
    activityApi.getTask().then(({ data }) => {
      if (data.errCode === codeConfig.errCode.SUCCESS_CODE) {
        that.setData({
          whetherFocus: data.data.whetherFocus,
          whetherCollect: data.data.whetherCollect,
          whetherOpen: data.data.whetherOpen,
          loadingFlag: false
        })
      } else {
        that.setData({
          loadingFlag: false
        })
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    }).catch(res => {
      that.setData({
        loadingFlag: false
      })
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
    })
  },
  // 关注
  onFollow(e) {
    let that = this
    if (that.data.hadAuthFlag) {
      let taskType = e.target.dataset.tasktype
      that.setData({
        taskType,
        mongoliaFlag: true
      })
      activityApi.doTask({ taskType })
    }
  },

  // 点击蒙层
  onMongolia() {
    this.setData({
      mongoliaFlag: false
    })
  },
  // 去开通达人店
  onOpenTalent() {
    wx.navigateTo({
      url: '../openTalent/openTalent'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})